
const gameWidth = gameDom.clientWidth;


class Pipe extends Rectangle {
    constructor(height, top, speed, dom) {
        super(52, height, gameWidth, top, speed, 0, dom)
        this.timer = null
    }


    onmove() {
        //判断边界值
        if (this.left < - this.width) {
            this.dom.remove()
        }
    }
}

function getRandom(min, max) {
    return Math.floor(Math.random() * (max - min) + min)
}

class PipePare {
    constructor(speed) {
        this.spaceHeigt = 150 //水管间隙的高度
        this.minHeight = 80 //水管最小的高度
        this.maxHeight = landTop - this.minHeight - this.spaceHeigt  //水管最大的高度
        const upHeight = getRandom(this.minHeight, this.maxHeight)

        const upDom = document.createElement("div")
        upDom.className = `pipe up`
        this.upPipe = new Pipe(upHeight, 0, speed, upDom)   //上水管

        const downHeight = landTop - upHeight - this.spaceHeigt
        const downTop = landTop - downHeight

        const downDom = document.createElement("div")
        downDom.className = `pipe down`
        this.downPipe = new Pipe(downHeight, downTop, speed, downDom)   //上水管

        gameDom.appendChild(upDom)
        gameDom.appendChild(downDom)
    }


    /**
     * 判断柱子是否移出视野
     */
    get useLess() {
        return this.upPipe.left < - this.upPipe.width
    }


    //让柱子移动
    move(duration) {
        this.upPipe.move(duration)
        this.downPipe.move(duration)
    }

}


// const pipePare = new PipePare(-150)

// /* 柱子不断的移动 */
// setInterval(() => {
//     pipePare.move(16 / 1000)
// }, 16)


/**
 * 用于不断的产生柱子对
 * pairs:记录多少个柱子对
 * tick:多长时间生成柱子对
 */
class PipePareProducer {
    constructor(speed) {
        this.speed = speed
        this.pairs = [];
        this.timer = null;
        this.tick = 1500;
    }

    //开始生产柱子对
    startProducer() {
        if (this.timer) return
        this.timer = setInterval(() => {
            //不断生产柱子
            this.pairs.push(new PipePare(this.speed))
            //移除掉用不到的柱子
            for (let i = 0; i < this.pairs.length; i++) {
                var pair = this.pairs[i];
                if (pair.useLess) {
                    //没用的柱子对
                    this.pairs.splice(i, 1);
                    i--;
                }
            }

        }, this.tick)

    }

    stopProduce() {
        clearInterval(this.timer);
        this.timer = null;
    }


}
// const pipePareProducer = new PipePareProducer(-100)
// pipePareProducer.startProducer()






/**
 * 这里能实现将 柱子对移动吗
 * 
 */

// setInterval(() => {
//     pipePareProducer.pairs.forEach(pair => {
//         pair.move(16 / 1000)
//     })
// }, 16)








